<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/6 0006
 * Time: 下午 4:54
 */

namespace app\admin\controller;


use think\console\command\make\Controller;
use think\Db;

/**
 * 资金分类
 * Class Cat_type
 * @package app\admin\controller
 */
class Cat_type extends Authinfo
{
    //资金分类列表
    public function index(){
        //模糊查询 项目名称
        $name=input('name');
        if($name){
            $map[]=array('name','=',$name);
        }
        // 分页
        $current=input('current');
        if(!$current){
            $current=1;
        }
        $pagenums=input('pagenums');
        if(!$pagenums){
            $pagenums=50;
        }
        //排序
        $order = input('order');
        if($order){
            $order='id DESC';
        }// 排序
        if($map){
            $total=Db::name('Cat_type')->where($map)->count('id');
            $info=Db::name('Cat_type')
                ->where($map)
                ->order($order)
                ->limit($nowpagenums,$pagenums)
                ->select();
        }else{
            $total=Db::name('Cat_type')->count('id');
            $info=Db::name('Cat_type')
                ->order($order)
                ->limit($nowpagenums,$pagenums)
                ->select();
        }

        if($info){
            $datas['status']=1;
            $datas['info']=$info;
            $datas['total']=$total;
            $datas['current']=$current;
        }else{
            $datas['status']=0;
            $datas['info']='无数据！';
            $datas['total']=$total;
            $datas['current']=$current;
        }
        return json($datas);
    }
    //资金添加
    public function index_add(){
        $post=input('param.');
        $info['name']=!empty($post['name'])?$post['name']:'';
        $info['pid']=!empty($post['pid'])?$post['pid']:'';
        $info['create_time']=!empty($post['create_time'])?$post['create_time']:'';
        $info['sort']=!empty($post['sort'])?$post['sort']:'';

        if($info){
            $result=Db::name('Cat_type')->insertAll($info);
            if($result){
                $datas['status']=1;
                $datas['info']='新增成功！';
            }else{
                $datas['status']=0;
                $datas['info']='无数据！';
            }
        }else{
            $datas['status']=0;
            $datas['info']='无数据';
        }
        return json($datas);
    }
    //资金修改
    public function index_edit(){
        $id=input('id');
        $post=input('param.');
        $info['name']=!empty($post['name'])?$post['name']:'';
        $info['pid']=!empty($post['pid'])?$post['pid']:'';
        $info['create_time']=!empty($post['create_time'])?$post['create_time']:'';
        $info['sort']=!empty($post['sort'])?$post['sort']:'';
        if($info){
            $result=Db::name('Cat_type')->where(['id'=>$id])->update($info);
            if($result){
                $datas['status']=1;
                $datas['info']='修改成功！';
            }else{
                $datas['status']=0;
                $datas['info']='无数据！';
            }
        }else{
            $datas['status']=0;
            $datas['info']='无数据';
        }
        return json($datas);
    }
    //资金删除
    public function index_delete(){
        $id=input('id');
        if($id){
            $info=Db::name('Cat_type')->where('id',$id)->delete();
            if($info){
                $datas['status']=1;
                $datas['info']=$info;
            }else{
                $datas['status']=0;
                $datas['info']='无数据！';
            }
        }else{
            $datas['status']=0;
            $datas['info']='无参数！';
        }
        return json($datas);
    }
}